<?php

/**
 * 后台广告管理模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: ad.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class adAction extends Action
{

    private $tmp_model;
    private $ad_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('ad');
        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_user');
    }

    /**
     * 广告管理 首页
     */
    public function index()
    {
        $uid = intval($_SESSION['admin_uid']);
        //TODO  取出所有广告
        $rs = $this->tmp_model->getAdList($uid);
        $this->V('ad');
    }

    /**
     * 新增/修改广告页面
     */
    public function add()
    {
        $uid = intval($_SESSION['admin_uid']);
        $ad_id = intval($this->getParam('ad_id'));
        if ($ad_id > 0) {
            $this->tmp_model->checkUserAd($ad_id, $uid);
            $editinfo = $this->tmp_model->getAdInfo($ad_id);            
            $img = $this->tmp_model->getCfgBody('img', $editinfo['ad_imgurls']);                        
            $imgurl_array = $img[3];
            $thumburl_array = $img[2];
            $thumbtitle_array = $img[1];            
        } else {
            $editinfo = array(
                'ad_id' => 0,
                'ad_title' => '',
                'ad_link' => 'http://',
                'ad_width' => '',
                'ad_height' => '',
                'ad_type_radio' => '',
                'ad_uploadfile' => '',
                'ad_order' => '',
                'ad_starttime' => '',
                'ad_endtime' => '',
                'ad_state_radio' => '',
                'ad_addtime' => 0,
                'ad_addusername' => '',
                'ad_externallinks' => '',
                'ad_area' => '',
                'ad_name' => '',
            );
            $imgurl_array = array();
            $thumbtitle_array = array();
            $thumburl_array = array();
        }
        //取广告类型option数组
        $ad_type_radio_ary = Tmac::config('ad.ad_type');
        $ad_type_radio_option = Utility::RadioButton($ad_type_radio_ary, 'type_radio', $editinfo['ad_type_radio'], 'onclick="typechange(this.value);"');

        //取广告状态option数组
        $ad_state_radio_ary = Tmac::config('ad.ad_state');
        $ad_state_radio_option = Utility::RadioButton($ad_state_radio_ary, 'state_radio', $editinfo['ad_state_radio'], 'onclick="statechange(this.value);"');


        //初始化一下    默认state_radio
        $editinfo['ad_starttime'] = !empty($editinfo['ad_starttime']) ? date('Y-m-d', $editinfo['ad_starttime']) : '';
        $editinfo['ad_endtime'] = !empty($editinfo['ad_endtime']) ? date('Y-m-d', $editinfo['ad_endtime']) : '';

        $this->assign('ad_type_radio_option', $ad_type_radio_option);
        $this->assign('ad_state_radio_option', $ad_state_radio_option);
        $this->assign('editinfo', $editinfo);
        $this->assign('imgurl_array', $imgurl_array);
        $this->assign('thumbtitle_array', $thumbtitle_array);
        $this->assign('thumburl_array', $thumburl_array);
        //TODO　载入资讯类别添加表单
        $this->V('ad');
    }

    /**
     * 广告管理 insert update => save()
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $ad_id = !empty($_POST['ad_id']) ? (int) $_POST['ad_id'] : 0;
        $area = !empty($_POST['area']) ? trim($this->H($_POST['area'])) : '';
        $title = !empty($_POST['title']) ? trim($this->H($_POST['title'])) : '';
        $sizewidth = !empty($_POST['sizewidth']) ? $_POST['sizewidth'] : '';
        $sizeheight = !empty($_POST['sizeheight']) ? $_POST['sizeheight'] : '';
        $link = !empty($_POST['link']) ? trim($this->H($_POST['link'])) : '';
        $ad_type_radio = !empty($_POST['type_radio']) ? $_POST['type_radio'] : '';

        $ad_externallinks = !empty($_POST['externallinks']) ? $_POST['externallinks'] : '';
        $ad_order = !empty($_POST['order']) && is_numeric($_POST['order']) ? $_POST['order'] : '0';
        $ad_state_radio = !empty($_POST['state_radio']) ? $_POST['state_radio'] : '';
        $ad_starttime = !empty($_POST['ad_starttime']) ? strtotime($_POST['ad_starttime']) : '';
        $ad_endtime = !empty($_POST['ad_endtime']) ? strtotime($_POST['ad_endtime']) : '';
        $ad_name = $this->H($this->getParam('ad_name'));
        $imgurl = $this->H($this->getParam('thumb'));
        $thumbdes = $this->H($this->getParam('thumbdes'));
        $thumburl = $this->H($this->getParam('thumburl'));


        $uid = intval($_SESSION['admin_uid']);
        /* 验证不能为空 */
        if (empty($area)) {
            $this->redirect('广告位置不能为空!');
            exit;
        }
        if (empty($title)) {
            $this->redirect('广告标题不能为空');
            exit;
        }
        if (empty($ad_name)) {
            $this->redirect('广告位置代号不能为空!');
            exit;
        }
        if (empty($link)) {
            $this->redirect('广告链接不能为空');
            exit;
        }

        $imgurls = '';        
        if (!empty($imgurl)) {
            $ad_uploadfile = $imgurl[0]; //第一张图片当做封面图片            
            if (is_array($imgurl)) {
                foreach ($imgurl AS $k => $v) {
                    $thumbtitle = $thumbdes[$k];
                    $thumburls = $thumburl[$k];
                    $imgurls.="{img key=\"$thumbtitle\" url=\"$thumburls\"}$v{/img}";
                }
            }
        } else {
            $ad_uploadfile = '';
        }

        // TODO goon to verify

        $info = array(
            'ad_title' => $title,
            'ad_width' => $sizewidth,
            'ad_height' => $sizeheight,
            'ad_link' => $link,
            'ad_type_radio' => $ad_type_radio,
            'ad_uploadfile' => $ad_uploadfile,
            'ad_order' => $ad_order,
            'ad_state_radio' => $ad_state_radio,
            'ad_starttime' => $ad_starttime,
            'ad_endtime' => $ad_endtime,
            'ad_area' => $area,
            'ad_addusername' => $_SESSION['admin'],
            'ad_externallinks' => $ad_externallinks,
            'ad_addtime' => time(),
            'ad_area' => $area,
            'ad_name' => $ad_name,
            'uid' => $uid,
            'ad_imgurls' => $imgurls
        );

        if ($ad_id > 0) {
            $this->tmp_model->checkUserAd($ad_id, $uid);
            $ad_page = HttpResponse::getCookie('ad_page');
            //update save article
            $where = 'ad_id = ' . $ad_id . '';
            $rs = $this->tmp_model->addAd($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改广告内容成功', PHP_SELF . '?m=ad&page=' . $ad_page . '');
            } else {
                $this->redirect('修改广告内容失败');
            }
        } else {
            //insert save article_class
            $rs = $this->tmp_model->addAd($info);
            if ($rs) {
                $this->redirect('添加广告成功', PHP_SELF . '?m=ad');
            } else {
                $this->redirect('添加广告失败');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $do_id = intval($this->getParam('ad_id'));
        $id_a = $this->getParam('id_a');
        $uid = intval($_SESSION['admin_uid']);

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
            foreach ($id_a AS $k => $v) {
                $this->tmp_model->checkUserAd($v, $uid);
            }
        } elseif (!empty($do_id)) {
            $id = $do_id;
            $this->tmp_model->checkUserAd($id, $uid);
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delAd($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $ad_page = HttpResponse::getCookie('ad_page');
            $this->redirect('删除广告成功', PHP_SELF . '?m=ad&page=' . $ad_page . '');
        } else {
            $this->redirect('删除广告失败，请重试！');
        }
    }

}